require(['loading'], function(loading) {
    var pageNo = 1;
    var pageSize = 100;
    //应用实例化
    var searchBody = new Vue({
        el: '#searchBody',
        data: {
            searchList: [],
            searchHistory: [],
            showLoading: false,
            apiUrl: wxUrl + '/v3.2/large/suggest',
            keyword:'',
            item: {
                params:{
                    token: token,
                    keyword: '',
                    pageNo : pageNo,
                    pageSize : pageSize
                }
            }
        },
        ready: function(){
            this.getSearchHistory();
        },
        methods:{
            getSearchHistory: function(){ //初始化
				//获取搜索历史
				var searchHistory = localStorage.getItem('blend_search_history');
				if(searchHistory != null && searchHistory != ''){
					var old_history = searchHistory.split(',');
					var new_history = [];
					for(var i = 0; i<old_history.length; i++){
						var items = old_history[i];
						if($.inArray(items,new_history) == -1){
							new_history.push(items);
						}
					}
                	this.searchHistory = new_history;
                }
            },
            clearMsg: function(e){ //清楚搜索框内文字
                $(e.target).hide();
                this.keyword = '';
                $('.content').val('');
            },
            clearHistory: function(){  //清除搜索历史纪录
            	localStorage.removeItem('blend_search_history');
            	this.searchHistory = [];
            },
            searchKeyup: function(){ //监听搜索框的keyup事件
            	$('.clear').show();
            },
            searchFun: function(){ //开始搜索
                this.item.params.keyword = this.keyword;
                if(this.keyword.length < 1){
                	return;
                }
                this.searchHistory.push(this.keyword);
                //存储本地搜索历史
                var oldSearchHistory = localStorage.getItem('blend_search_history');
                if(oldSearchHistory != null && oldSearchHistory != ''){
                	var newSearchHistory = oldSearchHistory + ',' + this.keyword;
                } else {
                	var newSearchHistory = this.keyword;
                }
                localStorage.setItem('blend_search_history',newSearchHistory);
                
                this.$http.get(this.apiUrl, this.item)
                .then(function(response) {
                    var result = JSON.parse(response.body);
					if(result.data.list.length > 0){
                    	var slist = result.data.list;
                        this.searchList = slist;
                        $('.seachHistory').hide();
                        $('.list').show();
                    } else {	                        	
                    	promptBox('无匹配的商品或供应商!');
                    }
                });
            },
            jumpDetail: function(type,id){ //点击跳转
                var jumpUrl = "";
                if(type==0){
                    jumpUrl = "../yellowPage/yellowPage_detail.html?id="+id;
                }else{
                    jumpUrl = "../bigSupply/bs_detail.html?id="+id;
                }
                window.location.href = jumpUrl;
            },
            clickHistory: function(e){
            	var keyword = $(e.target).text();
            	this.keyword = keyword;
            	this.searchFun();
            }
        }
    });
});

// 为可控制返回指定页面所有添加pushState
history.pushState({}, document.title, null);
pushVHistory();
window.addEventListener("popstate", function() {
    // 跳转到订单列表页面
	goBackVHistory();
});
